<?php
    
	class ForumView {
		
		private $_navigationView = null;
		private $_forumHandler = null;
		private $_postInput = "postInput";
		private $_postMessageInput = "postMessageInput";
		private $_editPostMessageInput = "editPostMessageInput";
		private $_newTopicInput ="newTopicInput";
		private $_newTopicNameInput = "newTopicNameInput";
		private $_newTopicMessageInput = "newTopicMessageInput";
		private $_editPostInput = "editPostInput";
		private $_numberOfItemsPerPage = 10;
		private $_uploadUserImage = "uploadUserImage";
		private $_upload = "upload";
		private $_pmInput = "pmInput";
		private $_pmMessageInput = "pmMessageInput";
		private $_pmTopicInput = "pmTopicInput";
		private $_pmRecieverInput = "pmRecieverInput";
		
		public function __construct($aNavigationView, $aForumHandler){
			$this->_navigationView = $aNavigationView;
			$this->_forumHandler = $aForumHandler;
		}
		
		public function DrawPosts($posts, $currentUser, $page, $isModerator){
			$ret = null;
			
			$page = ($page - 1)*10;

			$posts = array_slice($posts, $page, $this->_numberOfItemsPerPage, true);
			
			foreach ($posts as $post){
				$userimage = $this->_forumHandler->GetImageFromUsername($post->_username);
				
			 	$ret .= "<a name='$post->_id'id='$post->_id'></a>";
			 	$ret .= "<div class=postcontainer>";
				$ret .= "<div class=postmiddle><div class=userinfo>" . $post->_username . "<br/>Posts: " .
				$this->_forumHandler->GetPostCountFromUsername($post->_username) . 
				"<img src=UserImages/$userimage></div>";
				$ret .= "<div class=posttop>Posted: " . $post->_time . "</div>";
				$ret .= "<div class=postmessage>" .$post->_message . "</div></div>";
				if($currentUser == $post->_username || $isModerator == TRUE){
					$ret .= "<div class=postbottom>" . $this->_navigationView->DrawEditPostLink($post) . " ";
					$ret .= $this->_navigationView->DrawDeletePostLink($post) . "</div>";
				}
				else {
					$ret .= "<div class=postbottom></div>";					
				}
				$ret .= "</div>";
			}
		return $ret;
		}
		
		public function DrawTopics($topics, $page, $isModerator){
			$ret = null;
			
			$page = ($page - 1)*10;
			
			$arrayOfStickies = $topics;
			
			//Array of stickies		
			foreach($arrayOfStickies as $topic){
				if($topic->_sticky == 0){
					unset($arrayOfStickies[$topic->_id]);
				}
			}
			
			//Array of the non-stickies
			foreach($topics as $topic)
			{
				if($topic->_sticky == 1){
					unset($topics[$topic->_id]);
				}
			}
			
			//Reverse it to get the newer topics first
			krsort($topics);
			
			//Combine the two
			$topics = array_merge($arrayOfStickies,$topics);
			
			//Slice the array to get the correct posts for the page your viewing
			$topics = array_slice($topics, $page, $this->_numberOfItemsPerPage, true);

			foreach ($topics as $topic){
				$lastpost = $this->_forumHandler->GetLastPostFromTopic($topic->_id);
				$lastpostpage = $this->_forumHandler->GetWhichPagePostIsOn($topic->_id, $lastpost->_id);
				
			 	$ret .= "<div class=topiccontainer>";
				$ret .= "<div class=topicname>" . $this->_navigationView->DrawTopicLink($topic) . "</div>";
				$ret .= "<div class=topicusername>" . $topic->_username . "</div>";
				$ret .= "<div class=topicpostcount>" . $this->_forumHandler->GetNumberOfRepliesFromTopic($topic->_id) . "</div>";
				$ret .= "<div class=topiclastpost>" . $lastpost->_username . "<br/>"
				. $lastpost->_time . "</br>" . $this->_navigationView->DrawLastPostLink($topic->_id,$lastpost->_id, $lastpostpage) . "</div>";
				if($isModerator == TRUE)
				{
					$ret .= "<div class=delete>" . $this->_navigationView->DrawRemoveTopicLink($topic->_id) . "</div>";
					$ret .= "<div class=sticky>" . $this->_navigationView->DrawMakeStickyLink($topic->_id) . "</div>";
				}
				$ret .= "</div>";
			}
			
		return $ret;
		}
		
		public function DrawCategories($categories){
			$ret = null;
			
			foreach ($categories as $category){
				$lastpost = $this->_forumHandler->GetLastPostFromCategory($category->_id);
				$lastpostpage = $this->_forumHandler->GetWhichPagePostIsOn($lastpost->_topicid, $lastpost->_id);
				
				$ret .= "<div class=categorycontainer>";
			 	$ret .= "<div class=categoryname>" . $this->_navigationView->DrawCategoryLink($category);
				$ret .= "<div class=categorydescription>" . $category->_description . "</div></div>";
				$ret .= "<div class=categorytopics>" . $this->_forumHandler->GetNumberOfTopicsFromCategory($category->_id) . "</div>";
				$ret .= "<div class=categoryposts>" . $this->_forumHandler->GetNumberOfPostsFromCategory($category->_id) . "</div>";
				$ret .= "<div class=categorylastpost>" . $lastpost->_username . "</br>" . $lastpost->_time . 
				"</br>" . $this->_navigationView->DrawLastPostLink($lastpost->_topicid, $lastpost->_id, $lastpostpage) . "</div>";
				$ret .= "</div>";
			}
		return $ret;
		}
		
		public function DrawPrivateMessagesTopics($messages){
			$ret = null;
			
			krsort($messages);
			
			foreach ($messages as $message){
				$ret .= "<div class=privatemessegestopiccontainer>";
				$ret .= "<div class=privatemessegesender>" . $message->_sender . "</div>";
				$ret .= "<div class=privatemessegereciever>" . $message->_receiver . "</div>";
				$ret .= "<div class=privatemessegetopic>" . $this->_navigationView->DrawPrivateMessageTopicLink($message) . "</div>"; 
				$ret .= "<div class=privatemessegetime>" . $message->_time . "</div>";
				$ret .= "</div>";
			}
			
			return $ret;
		}
		
		public function DrawPrivateMessage($message, $currentuser){
			
			if($currentuser == $message->_sender)
			{
				$timetopic = "Sent: ";
			}else {
				$timetopic = "Recieved: ";
			}
			
			$userimage = $this->_forumHandler->GetImageFromUsername($message->_sender);
			
			$ret = "<div class=postcontainer>";
			$ret .= $this->DrawPostTop();
			$ret .= "<div class=postmiddle><div class=userinfo>" . $message->_sender . "<br/>Posts: " .
			$this->_forumHandler->GetPostCountFromUsername($message->_sender) . 
			"<img src=UserImages/$userimage></div>";
			$ret .= "<div class=posttop>" . $timetopic . $message->_time . "</div>";
			$ret .= "<div class=postmessage>" .$message->_message . "</div></div>";
			$ret .= "<div class=postbottom></div>";
			$ret .= "</div>";
				
			return $ret;
		}
			
		public function DrawTopicTop(){
			$ret = null;
			
			$ret .= "<div class=topictopcontainer>";
			$ret .= "<div class=topictoptopics>Topics</div>";
			$ret .= "<div class=topictopauthor>Author</div>";
			$ret .= "<div class=topictopreplies>Replies</div>";
			$ret .= "<div class=topictoplastpost>Last Post</div>";
			$ret .= "</div>";
			
			return $ret;
		}
		public function DrawCategoryTop(){
			$ret = null;
			
			$ret .= "<div class=categorytopcontainer>";
			$ret .= "<div class=categorytopcategories>Categories</div>";
			$ret .= "<div class=categorytoptopics>Topics</div>";
			$ret .= "<div class=categorytopposts>Posts</div>";
			$ret .= "<div class=categorytoplastpost>Last Post</div>";
			$ret .= "</div>";
			
			return $ret;
		}
		public function DrawPostTop(){
			$ret = null;
			
			$ret .= "<div class=posttopcontainer>";
			$ret .= "<div class=posttopauthor>Author</div>";
			$ret .= "<div class=posttopmessage>Message</div>";
			$ret .= "</div>";
			
			return $ret;
		}
		public function DrawPrivateMessagesTopicsTop()
		{
			$ret = "<div class=privatemessegestopictopcontainer><p>Private Messages</p>";
			$ret .= "<div class=privatemessegestopiccontainer>";
			$ret .= "<div class=privatemessegesender>Sender</div>";
			$ret .= "<div class=privatemessegereciever>Receiver</div>";
			$ret .= "<div class=privatemessegetopic>Topic</div>";
			$ret .= "<div class=privatemessegetime>Time</div>";
			$ret .= "</div></div>";
			
			return $ret;
		}
		
		public function DrawTopicHeader($topic){
			return "<div class=topicheader>$topic->_name</div>";
		}
		
		public function DrawCategoryHeader($category){
			return "<div class=categoryheader>$category->_name</div>";
		}
		
		public function DrawForumHeader(){
			return "<a href='index.php'><img src='Images/aReallyCoolForumBanner.png' alt='A Really Cool Forum' /></a>";
		}
		
		public function DrawUserHeader($username){
			return "<div class=categoryheader>$username</div>";
		}
		
		public function DrawNewPostForm(){
			$activeTopic = $this->_navigationView->GetActiveTopic();
			$activeTopicString = $this->_navigationView->GetActiveTopicString();
			$activePage = $this->_navigationView->GetActivePage();
			$activePageString = $this->_navigationView->GetActivePageString();
			
			return "<div class=newpostcontainer><a name='addpost' id='addpost'></a><form method='POST' action='index.php?$activeTopicString=$activeTopic&$activePageString=$activePage#addpost'>
		<TEXTAREA NAME=$this->_postMessageInput></TEXTAREA><br/>
		<input type='submit' name=$this->_postInput value='Add New Post'/>
		</form></div>";
		}
		
		public function DrawEditPostForm($message){
			$activePostString = $this->_navigationView->GetActivePostString();
			$activePost = $this->_navigationView->GetActivePost();
			$activeTopic = $this->_navigationView->GetActiveTopic();
			$activeTopicString = $this->_navigationView->GetActiveTopicString();
			$activePage = $this->_navigationView->GetActivePage();
			$activePageString = $this->_navigationView->GetActivePageString();
			
			return "<div class=newpostcontainer><a name='editpost' id='editpost'></a><form method='POST' action='index.php?$activePostString=$activePost&$activeTopicString=$activeTopic&$activePageString=$activePage#$activePost'>
			<TEXTAREA NAME=$this->_editPostMessageInput>$message</TEXTAREA><br/>
			<input type='submit' name=$this->_editPostInput value='Edit Post'/>
			</form></div>";
		}
		
		public function DrawNewTopicForm(){
			$activeCategoryString = $this->_navigationView->GetActiveCategoryString();
			$activeCategory = $this->_navigationView->GetActiveCategory();
			return "<div class=newtopiccontainer><form method='POST' action='index.php?$activeCategoryString=$activeCategory'>
		<div class=newtopicname>Topic Name:</div><div class=newtopicinput> <input type='text'  maxlength='64' name=$this->_newTopicNameInput></input></div>
		<div class=newtopicmessage>Topic Message:</div><div class=newtopictextarea><TEXTAREA NAME=$this->_newTopicMessageInput></TEXTAREA></div>
		<div class=newtopicbutton><input type='submit' name=$this->_newTopicInput value='Add New Topic'/></div>
		</form></div>";
		}
		
		public function DrawNewPmForm(){
			$activeControllerString = $this->_navigationView->GetActiveControllerString();
			return "<div class=newtopiccontainer><form method='POST' action='index.php?$activeControllerString=UserOptions'>
			<div class=newtopicname>Receiver:</div><div class=newtopicinput> <input type='text'  maxlength='32' name=$this->_pmRecieverInput></input></div>
		<div class=newtopicname>Topic:</div><div class=newtopicinput> <input type='text'  maxlength='60' name=$this->_pmTopicInput></input></div>
		<div class=newtopicmessage>Private Message:</div><div class=newtopictextarea><TEXTAREA NAME=$this->_pmMessageInput></TEXTAREA></div>
		<div class=newtopicbutton><input type='submit' name=$this->_pmInput value='Send Message'/></div>
		</form></div>";
		}
		
		public function DrawUploadUserImageForm(){

			return "<form method='post' enctype='multipart/form-data'>
			<div class=userpageimage>Change User Image</div>
			<div class=image><input type='file' name=$this->_uploadUserImage id='file' /> </div>
			<div class=imagebutton><input type='submit' name=$this->_upload value='Upload' /></div>
			</form>";
		
		}
		public function DrawUserPage($username){
			$userimage = $this->_forumHandler->GetImageFromUsername($username);
			return "<div class=userimagecontainer><div class=userpageimage>Current User Image: </div><div class=image><img src=UserImages/$userimage>" . $this->DrawUploadUserImageForm() . "</div><div class=imagehelp>Your picture needs to be less than 1mb in size.</br> Allowed formats: .gif/.jpg/.png</div></div>";
		}
		
		public function DrawNewTopicHeader(){
			return "<div class=newtopicheader>Add a new topic</div>";
		}
		
		public function DrawNewPostHeader(){
			return "<div class=newpostheader>Add a new post</div>";
		}
		
		public function DrawEditPostHeader(){
			return "<div class=newpostheader>Edit post</div>";
		}
		
		public function DrawUserPageHeader()
		{
			return "<div class=newtopicheader>User Page</div>";
		}
		
		public function DrawPrivateMessageHeader()
		{
			return "<div class=newtopicheader>Private Message</div>";
		}
		
		
		public function DrawNewLoginHeader($isloggedin){
			if($isloggedin == false)
			{
				return "<div class=loginheader>Please log in to access the forum</div>";
			}
			return null;
		}
		
		public function AddUserPageDiv(){
			return "<div class=userpagecontainer>";
		}
		
		public function AddEndDiv(){
			return "</div>";
		}
		
		public function AddForumDiv(){
			return "<div id=forum>";
		}
		
		public function TriedToAddNewPost(){
			if (isset( $_POST[$this->_postInput]) == true) {
				return true;
			}
			return false;
		}	
		
		public function TriedToAddNewTopic(){
			if (isset( $_POST[$this->_newTopicInput]) == true) {
				return true;
			}
			return false;
		}	
		public function TriedToSendPm(){
			if (isset( $_POST[$this->_pmInput]) == true){
				return true;
			}
			return false;
		}
		//Function to see if the user is trying to upload a userimage.
		function TriedToUploadImage(){
			if(isset($_POST[$this->_upload])){
				return true;
			}
			return false;
		}
		
		public function TriedToEditPost(){
			if (isset( $_POST[$this->_editPostInput]) == true) {
				return true;
			}
			return false;
		}
		
		public function GetNewPmReciever(){
			if (isset($_POST[$this->_pmRecieverInput])){
				return $_POST[$this->_pmRecieverInput];
			}
			return false;
		}
		
		public function GetNewPmTopic(){
			if (isset($_POST[$this->_pmTopicInput])){
				return $_POST[$this->_pmTopicInput];
			}
			return false;
		}
		
		public function GetNewPmMessage(){
			if (isset($_POST[$this->_pmMessageInput])){
				return $_POST[$this->_pmMessageInput];
			}
			return false;
		}
		
		public function GetNewPostMessage(){
			if (isset($_POST[$this->_postMessageInput])){
				return $_POST[$this->_postMessageInput];
			}
			return false;
		}
		
		public function GetEditPostMessage(){
			if (isset($_POST[$this->_editPostMessageInput])){
				return $_POST[$this->_editPostMessageInput];
			}
			return false;
		}
		
		public function GetNewTopicMessage(){
			if (isset($_POST[$this->_newTopicMessageInput])){
				return $_POST[$this->_newTopicMessageInput];
			}
			return false;
		}
		public function GetNewTopicName(){
			if (isset($_POST[$this->_newTopicNameInput])){
				return $_POST[$this->_newTopicNameInput];
			}
			return false;
		}
		//Returns the file the user is currently trying to upload
		function GetUploadedFile(){
			if(isset($_FILES[$this->_uploadUserImage])){
				return $_FILES[$this->_uploadUserImage];
			}
			return null;
		}
			
		public function CheckTopicInfo($message,$topicname){
			
			if(strlen($topicname) == 0){
				return "<div class=helpmessage>You need to write a name for your topic</div>";
			}
			if($this->CheckMessageInfo($message) != false){
				return $this->CheckMessageInfo($message);
			}
			return false;
		}
		
		public function CheckMessageInfo($message){
			if(strlen($message) < 10){
				return "<div class=helpmessage>You need to have atleast 10 characters in your message</div>";
			}
			return false;
		}
		
		public function CheckImageInfo($file){
			
			if($file["size"] > 1000000){
				return "<div class=helpmessage>The image is too large</div>";
			}
			if(($file["type"] != "image/png") && ($file["type"] != "image/jpeg") && ($file["type"] != "image/gif")){
				return "<div class=helpmessage>The image is in an incorrect format</div>";
			}
			return false;
		}
		
		public function CheckPrivateMessageInfo($receiver,$topic,$message){
			if(strlen($receiver) == 0){
				return "<div class=helpmessage>You need to specify a receiver for your message</div>";
			}
			if(strlen($topic) == 0){
				return "<div class=helpmessage>You need to write a topic</div>";
			}
			if($this->CheckMessageInfo($message) != false){
				return $this->CheckMessageInfo($message);
			}
			return false;
		}
	}